<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Python杂记 | whoway</title>
    <meta name="description" content="Personal Blog Website">
    <link rel="icon" href="/images/photo.jpg">
  <link rel="manifest" href="/images/photo.jpg">
  <link rel="apple-touch-icon" href="/images/photo.jpg">
  <meta http-quiv="pragma" cotent="no-cache">
  <meta http-quiv="pragma" cotent="no-cache,must-revalidate">
  <meta http-quiv="expires" cotent="0">
    
    <link rel="preload" href="/assets/css/0.styles.0dbae9ec.css" as="style"><link rel="preload" href="/assets/js/app.c70e21ad.js" as="script"><link rel="preload" href="/assets/js/42.76f52d62.js" as="script"><link rel="prefetch" href="/assets/js/10.15222a53.js"><link rel="prefetch" href="/assets/js/100.7e0e5a86.js"><link rel="prefetch" href="/assets/js/101.efd59f25.js"><link rel="prefetch" href="/assets/js/102.dfbdc06c.js"><link rel="prefetch" href="/assets/js/103.d3ab2109.js"><link rel="prefetch" href="/assets/js/104.117957ef.js"><link rel="prefetch" href="/assets/js/105.046e8ff3.js"><link rel="prefetch" href="/assets/js/106.aebdc17d.js"><link rel="prefetch" href="/assets/js/107.248733c2.js"><link rel="prefetch" href="/assets/js/108.a2fecadc.js"><link rel="prefetch" href="/assets/js/109.35196857.js"><link rel="prefetch" href="/assets/js/11.770642f2.js"><link rel="prefetch" href="/assets/js/110.cf3d973c.js"><link rel="prefetch" href="/assets/js/111.f985889a.js"><link rel="prefetch" href="/assets/js/112.ad614f41.js"><link rel="prefetch" href="/assets/js/113.f666653c.js"><link rel="prefetch" href="/assets/js/114.c6c3f384.js"><link rel="prefetch" href="/assets/js/115.e51d3c2f.js"><link rel="prefetch" href="/assets/js/116.4f4b39f5.js"><link rel="prefetch" href="/assets/js/117.99352e11.js"><link rel="prefetch" href="/assets/js/118.c6ae6572.js"><link rel="prefetch" href="/assets/js/119.4ccbe778.js"><link rel="prefetch" href="/assets/js/12.042a92ff.js"><link rel="prefetch" href="/assets/js/120.edda1c4f.js"><link rel="prefetch" href="/assets/js/121.30a638ed.js"><link rel="prefetch" href="/assets/js/122.6efcefb1.js"><link rel="prefetch" href="/assets/js/123.91e6665b.js"><link rel="prefetch" href="/assets/js/124.f27e3d7e.js"><link rel="prefetch" href="/assets/js/125.c75712d5.js"><link rel="prefetch" href="/assets/js/126.ed756cce.js"><link rel="prefetch" href="/assets/js/127.2f06c74c.js"><link rel="prefetch" href="/assets/js/128.d5f6f30e.js"><link rel="prefetch" href="/assets/js/129.508b7eed.js"><link rel="prefetch" href="/assets/js/13.b5280c37.js"><link rel="prefetch" href="/assets/js/130.dc05f9aa.js"><link rel="prefetch" href="/assets/js/131.e0ba69b1.js"><link rel="prefetch" href="/assets/js/132.d79bcaa4.js"><link rel="prefetch" href="/assets/js/133.34acc01a.js"><link rel="prefetch" href="/assets/js/134.dabf64d5.js"><link rel="prefetch" href="/assets/js/135.ad90c915.js"><link rel="prefetch" href="/assets/js/136.dbb0074f.js"><link rel="prefetch" href="/assets/js/137.284ad365.js"><link rel="prefetch" href="/assets/js/138.a4b6856f.js"><link rel="prefetch" href="/assets/js/139.c9c1e20f.js"><link rel="prefetch" href="/assets/js/14.df02ba38.js"><link rel="prefetch" href="/assets/js/140.8b0a9269.js"><link rel="prefetch" href="/assets/js/141.9c7759c5.js"><link rel="prefetch" href="/assets/js/142.a4201a82.js"><link rel="prefetch" href="/assets/js/143.d7da6e8c.js"><link rel="prefetch" href="/assets/js/144.5e48e65d.js"><link rel="prefetch" href="/assets/js/145.a0e2633c.js"><link rel="prefetch" href="/assets/js/146.3c775f9b.js"><link rel="prefetch" href="/assets/js/147.22add89a.js"><link rel="prefetch" href="/assets/js/148.cfde1009.js"><link rel="prefetch" href="/assets/js/149.ffc835b5.js"><link rel="prefetch" href="/assets/js/15.fbdfc4ee.js"><link rel="prefetch" href="/assets/js/150.406c4b20.js"><link rel="prefetch" href="/assets/js/151.b2040eea.js"><link rel="prefetch" href="/assets/js/152.7bc65661.js"><link rel="prefetch" href="/assets/js/153.1d7c65e3.js"><link rel="prefetch" href="/assets/js/154.1309de49.js"><link rel="prefetch" href="/assets/js/155.81d3ee1f.js"><link rel="prefetch" href="/assets/js/156.154a4ef2.js"><link rel="prefetch" href="/assets/js/16.e5eb6147.js"><link rel="prefetch" href="/assets/js/17.57853c4a.js"><link rel="prefetch" href="/assets/js/18.cb9d7518.js"><link rel="prefetch" href="/assets/js/19.f354dc47.js"><link rel="prefetch" href="/assets/js/2.570d8a23.js"><link rel="prefetch" href="/assets/js/20.b5af7fad.js"><link rel="prefetch" href="/assets/js/21.0b1928fe.js"><link rel="prefetch" href="/assets/js/22.f78666de.js"><link rel="prefetch" href="/assets/js/23.29c3f366.js"><link rel="prefetch" href="/assets/js/24.6f596516.js"><link rel="prefetch" href="/assets/js/25.14067b60.js"><link rel="prefetch" href="/assets/js/26.74ba4989.js"><link rel="prefetch" href="/assets/js/27.13d60edd.js"><link rel="prefetch" href="/assets/js/28.9523cb32.js"><link rel="prefetch" href="/assets/js/29.8ec842e9.js"><link rel="prefetch" href="/assets/js/3.3fb3d2e0.js"><link rel="prefetch" href="/assets/js/30.805597a8.js"><link rel="prefetch" href="/assets/js/31.831b195d.js"><link rel="prefetch" href="/assets/js/32.063c672d.js"><link rel="prefetch" href="/assets/js/33.6d93fac3.js"><link rel="prefetch" href="/assets/js/34.56e8263c.js"><link rel="prefetch" href="/assets/js/35.dbe688bb.js"><link rel="prefetch" href="/assets/js/36.dc5af2c1.js"><link rel="prefetch" href="/assets/js/37.0a7494f6.js"><link rel="prefetch" href="/assets/js/38.fe4fc171.js"><link rel="prefetch" href="/assets/js/39.f5ed5e92.js"><link rel="prefetch" href="/assets/js/4.2c405ec8.js"><link rel="prefetch" href="/assets/js/40.fe7e2714.js"><link rel="prefetch" href="/assets/js/41.30b0811d.js"><link rel="prefetch" href="/assets/js/43.e7bb0817.js"><link rel="prefetch" href="/assets/js/44.ead0e883.js"><link rel="prefetch" href="/assets/js/45.235df046.js"><link rel="prefetch" href="/assets/js/46.5f09e829.js"><link rel="prefetch" href="/assets/js/47.67116354.js"><link rel="prefetch" href="/assets/js/48.31f34543.js"><link rel="prefetch" href="/assets/js/49.10b5ebba.js"><link rel="prefetch" href="/assets/js/5.6f47322c.js"><link rel="prefetch" href="/assets/js/50.c0f0b7f1.js"><link rel="prefetch" href="/assets/js/51.5143f3bf.js"><link rel="prefetch" href="/assets/js/52.eeddfd48.js"><link rel="prefetch" href="/assets/js/53.eb790db5.js"><link rel="prefetch" href="/assets/js/54.8fe5421c.js"><link rel="prefetch" href="/assets/js/55.d8f9004b.js"><link rel="prefetch" href="/assets/js/56.62ac9b92.js"><link rel="prefetch" href="/assets/js/57.a9caec0d.js"><link rel="prefetch" href="/assets/js/58.f93fc522.js"><link rel="prefetch" href="/assets/js/59.a81a03aa.js"><link rel="prefetch" href="/assets/js/6.8c2ea393.js"><link rel="prefetch" href="/assets/js/60.ab782775.js"><link rel="prefetch" href="/assets/js/61.6dd12daf.js"><link rel="prefetch" href="/assets/js/62.76f4b01f.js"><link rel="prefetch" href="/assets/js/63.6f8a4742.js"><link rel="prefetch" href="/assets/js/64.6f8bb1fa.js"><link rel="prefetch" href="/assets/js/65.4120a44b.js"><link rel="prefetch" href="/assets/js/66.360c2d2b.js"><link rel="prefetch" href="/assets/js/67.26f84d32.js"><link rel="prefetch" href="/assets/js/68.68f45e5e.js"><link rel="prefetch" href="/assets/js/69.e311eb56.js"><link rel="prefetch" href="/assets/js/7.6762b2d7.js"><link rel="prefetch" href="/assets/js/70.cea82674.js"><link rel="prefetch" href="/assets/js/71.783ddcf7.js"><link rel="prefetch" href="/assets/js/72.e5467385.js"><link rel="prefetch" href="/assets/js/73.b8fb681b.js"><link rel="prefetch" href="/assets/js/74.1bae37db.js"><link rel="prefetch" href="/assets/js/75.024387e5.js"><link rel="prefetch" href="/assets/js/76.a8e53010.js"><link rel="prefetch" href="/assets/js/77.8c55500a.js"><link rel="prefetch" href="/assets/js/78.7ce90bf5.js"><link rel="prefetch" href="/assets/js/79.ef71713f.js"><link rel="prefetch" href="/assets/js/8.788a6364.js"><link rel="prefetch" href="/assets/js/80.acad589d.js"><link rel="prefetch" href="/assets/js/81.02670d10.js"><link rel="prefetch" href="/assets/js/82.53b7b1ac.js"><link rel="prefetch" href="/assets/js/83.99eb8581.js"><link rel="prefetch" href="/assets/js/84.d1535ce3.js"><link rel="prefetch" href="/assets/js/85.fe2b7de9.js"><link rel="prefetch" href="/assets/js/86.41850272.js"><link rel="prefetch" href="/assets/js/87.1cdc6df9.js"><link rel="prefetch" href="/assets/js/88.01bf3461.js"><link rel="prefetch" href="/assets/js/89.17c69819.js"><link rel="prefetch" href="/assets/js/9.3813842d.js"><link rel="prefetch" href="/assets/js/90.f6ae7e35.js"><link rel="prefetch" href="/assets/js/91.507bc284.js"><link rel="prefetch" href="/assets/js/92.90551782.js"><link rel="prefetch" href="/assets/js/93.dc442d78.js"><link rel="prefetch" href="/assets/js/94.315f4e94.js"><link rel="prefetch" href="/assets/js/95.ccd6c6bf.js"><link rel="prefetch" href="/assets/js/96.0c6d89d0.js"><link rel="prefetch" href="/assets/js/97.1a9f10a9.js"><link rel="prefetch" href="/assets/js/98.43be3caa.js"><link rel="prefetch" href="/assets/js/99.54c8207b.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0dbae9ec.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">whoway</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link router-link-active">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link router-link-active">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>Python杂记</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#✅基础对比" class="sidebar-link">✅基础对比</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_01-linux下的python" class="sidebar-link">01.Linux下的python</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_02-关于空格「这种没影响」和print" class="sidebar-link">02.关于空格「这种没影响」和print</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_03-python中的-和" class="sidebar-link">03.Python中的''和&quot;&quot;</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_04-注释和linux的一致性" class="sidebar-link">04.注释和Linux的一致性#</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_05-关于缩进「有影响了」" class="sidebar-link">05.关于缩进「有影响了」</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#好处" class="sidebar-link">好处</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#坏处" class="sidebar-link">坏处</a></li></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_06-在python中，能够直接处理的数据类型有以下几种" class="sidebar-link">06.在Python中，能够直接处理的数据类型有以下几种</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_6-1-整数" class="sidebar-link">6.1.整数</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_6-2-浮点数" class="sidebar-link">6.2.浮点数</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_6-3-字符串" class="sidebar-link">6.3.字符串</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_6-4-布尔值" class="sidebar-link">6.4.布尔值</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_6-5-空值" class="sidebar-link">6.5.空值</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#变量" class="sidebar-link">变量</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#变量名》" class="sidebar-link">变量名》</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#python是动态语言" class="sidebar-link">Python是动态语言</a></li></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_07-常量" class="sidebar-link">07.常量</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_08-python中2种除法" class="sidebar-link">08.Python中2种除法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#一种除法是-：" class="sidebar-link">一种除法是/：</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#还有一种除法是-，称为地板除" class="sidebar-link">还有一种除法是//，称为地板除</a></li></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#字符串和编码" class="sidebar-link">字符串和编码</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#我们就可以总结一下现在计算机系统通用的字符编码工作方式：" class="sidebar-link">我们就可以总结一下现在计算机系统通用的字符编码工作方式：</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#python的字符串" class="sidebar-link">Python的字符串</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#encode和decode函数" class="sidebar-link">encode和decode函数</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#len函数" class="sidebar-link">len函数</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#格式化" class="sidebar-link">格式化</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#format-「我不打算用这个」" class="sidebar-link">format()「我不打算用这个」</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#f-string「我也还是不常用这个」" class="sidebar-link">f-string「我也还是不常用这个」</a></li></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#✅判断和循环" class="sidebar-link">✅判断和循环</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#条件判断" class="sidebar-link">条件判断</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#if语句" class="sidebar-link">if语句</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#跟其它语言有所区别，python中并没有switch-case语句" class="sidebar-link">跟其它语言有所区别，Python中并没有Switch/Case语句!</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#input函数的纠偏" class="sidebar-link">input函数的纠偏</a></li></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#循环" class="sidebar-link">循环</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#for-in循环" class="sidebar-link">for...in循环</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#while循环" class="sidebar-link">while循环</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#在-python-中没有-do-while-循环！" class="sidebar-link">在 Python 中没有 do..while 循环！</a></li></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#✅函数" class="sidebar-link">✅函数</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#help函数的使用" class="sidebar-link">help函数的使用</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#数据类型转换" class="sidebar-link">数据类型转换</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_1-定义函数" class="sidebar-link">1.定义函数</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#_2-函数的参数" class="sidebar-link">2.函数的参数</a></li><li class="sidebar-sub-header"><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#import的用法在后续模块一节中会详细介绍。" class="sidebar-link">import的用法在后续模块一节中会详细介绍。</a></li></ul></li><li><a href="/01.Language/Python/01.Python3%E5%92%8C%E5%85%B6%E4%BB%96%E8%AF%AD%E8%A8%80%E5%AF%B9%E6%AF%94.html#✅模块" class="sidebar-link">✅模块</a><ul class="sidebar-sub-headers"></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="python杂记"><a href="#python杂记" class="header-anchor">#</a> Python杂记</h1> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>&lt;font style=&quot;background:yellow&quot;&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="✅基础对比"><a href="#✅基础对比" class="header-anchor">#</a> ✅基础对比</h2> <h2 id="_01-linux下的python"><a href="#_01-linux下的python" class="header-anchor">#</a> 01.Linux下的python</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#!/usr/bin/env python3</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'111'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>由于Python源代码也是一个文本文件，所以，当你的源代码中包含中文的时候，在保存源代码时，就需要务必指定保存为UTF-8编码。当Python解释器读取源代码时，<font style="background:pink;">为了让它按UTF-8编码读取，我们通常在文件开头写上这两行</font>：</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#!/usr/bin/env python3</span>
<span class="token comment"># -*- coding: utf-8 -*-</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_02-关于空格「这种没影响」和print"><a href="#_02-关于空格「这种没影响」和print" class="header-anchor">#</a> 02.关于空格「这种没影响」和print</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 下面有空格</span>
name <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>和下面是一样的！</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 下面没有空格，但是不影响这个函数的使用</span>
name<span class="token operator">=</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>我观察到，<font style="background:pink;">Python中print的通用程度，类似到C++中的<code>cout</code></font></li></ul> <h2 id="_03-python中的-和"><a href="#_03-python中的-和" class="header-anchor">#</a> 03.Python中的<code>''</code>和<code>&quot;&quot;</code></h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">//</span>一般写成下面这样
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'111'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>但是，下面也可以？</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;111&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_04-注释和linux的一致性"><a href="#_04-注释和linux的一致性" class="header-anchor">#</a> 04.注释和Linux的一致性<code>#</code></h2> <h2 id="_05-关于缩进「有影响了」"><a href="#_05-关于缩进「有影响了」" class="header-anchor">#</a> 05.关于缩进「有影响了」</h2> <ul><li>每一行都是一个语句，<font style="background:yellow;">当语句以冒号<code>:</code>结尾时，<strong>缩进</strong>的语句视为代码块</font>。</li> <li>Python使用缩进来组织代码块，请务必遵守约定俗成的习惯，坚持使用4个空格的缩进。</li> <li>在文本编辑器中，需要设置把Tab自动转换为4个空格，确保不混用Tab和空格。</li></ul> <blockquote><p>缩进有利有弊。</p></blockquote> <h3 id="好处"><a href="#好处" class="header-anchor">#</a> 好处</h3> <ul><li>好处是强迫你写出格式化的代码，但没有规定缩进是几个空格还是Tab。按照约定俗成的惯例，应该始终坚持使用<em>4个空格</em>的缩进。</li> <li>另一个好处是强迫你写出缩进较少的代码，你会倾向于把一段很长的代码拆分成若干函数，从而得到缩进较少的代码。</li></ul> <h3 id="坏处"><a href="#坏处" class="header-anchor">#</a> 坏处</h3> <ul><li>“复制－粘贴”功能失效了，这是最坑爹的地方。当你重构代码时，粘贴过去的代码必须重新检查缩进是否正确。此外，IDE很难像格式化Java代码那样格式化Python代码</li></ul> <blockquote><p>Python程序和Java和C++一样是<strong>大小写敏感</strong>的，如果写错了大小写，程序会报错。</p></blockquote> <h2 id="_06-在python中，能够直接处理的数据类型有以下几种"><a href="#_06-在python中，能够直接处理的数据类型有以下几种" class="header-anchor">#</a> 06.在Python中，能够直接处理的数据类型有以下几种</h2> <h3 id="_6-1-整数"><a href="#_6-1-整数" class="header-anchor">#</a> 6.1.整数</h3> <ul><li>有时候用十六进制表示整数比较方便，十六进制用<code>0x</code>前缀和0-9，a-f表示</li> <li>对于很大的数，例如<code>10000000000</code>，很难数清楚0的个数。Python允许在数字中间以<code>_</code>分隔</li></ul> <blockquote><p>注意：<font style="background:yellow;">Python的整数没有大小限制</font>，而某些语言的整数根据其存储长度是有大小限制的，例如Java对32位整数的范围限制在<code>-2147483648</code>-<code>2147483647</code>。</p></blockquote> <h3 id="_6-2-浮点数"><a href="#_6-2-浮点数" class="header-anchor">#</a> 6.2.浮点数</h3> <ul><li>对于很大或很小的浮点数，就必须用科学计数法表示，把10用e替代，1.23x109就是<code>1.23e9</code>，或者<code>12.3e8</code>，0.000012可以写成<code>1.2e-5</code>，等等</li> <li>对于很大或很小的浮点数，就必须用科学计数法表示，把10用e替代，1.23x109就是<code>1.23e9</code>，或者<code>12.3e8</code>，0.000012可以写成<code>1.2e-5</code>，等等</li> <li><font style="background:yellow;">整数和浮点数在计算机内部存储的方式是不同的，整数运算永远是精确的（除法难道也是精确的？是的！），而浮点数运算则可能会有四舍五入的误差。</font></li></ul> <blockquote><p>Python的浮点数也没有大小限制，但是超出一定范围就直接表示为<code>inf</code>（无限大）。</p></blockquote> <h3 id="_6-3-字符串"><a href="#_6-3-字符串" class="header-anchor">#</a> 6.3.字符串</h3> <ul><li><p><font style="background:yellow;">字符串是以单引号<code>'</code>或双引号<code>&quot;</code>括起来的任意文本</font>，比如<code>'abc'</code>，<code>&quot;xyz&quot;</code>等等。请注意，<code>''</code>或<code>&quot;&quot;</code>本身只是一种表示方式，不是字符串的一部分</p> <ul><li>233：我要是用<code>&quot; &quot;</code>写，岂不是暴露我是<code>C++</code>技术栈出身的了，233</li></ul></li> <li><p><font style="background:pink;">如果<code>'</code>本身也是一个字符，那就可以用<code>&quot;&quot;</code>括起来</font>，比如<code>&quot;I'm OK&quot;</code>包含的字符是<code>I</code>，<code>'</code>，<code>m</code>，空格，<code>O</code>，<code>K</code>这6个字符。</p></li> <li><p><font style="background:pink;">如果字符串内部既包含<code>'</code>又包含<code>&quot;</code>怎么办？可以用转义字符<code>\</code>来标识，比如：</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token string">'I\'m \&quot;OK\&quot;!'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>转义字符<code>\</code>可以转义很多字符，比如<code>\n</code>表示换行，<code>\t</code>表示制表符，字符<code>\</code>本身也要转义，所以<code>\\</code>表示的字符就是<code>\</code></p></li> <li><p>如果字符串里面有很多字符都需要转义，就需要加很多<code>\</code>，为了简化，<font style="background:pink;">Python还允许用<code>r''</code>表示<code>''</code>内部的字符串默认不转义</font></p> <ul><li><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">r'\\\t\\'</span><span class="token punctuation">)</span>
\\\t\\
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul></li></ul> <h3 id="_6-4-布尔值"><a href="#_6-4-布尔值" class="header-anchor">#</a> 6.4.布尔值</h3> <ul><li>布尔值和布尔代数的表示完全一致，一个布尔值只有<code>True</code>、<code>False</code><font style="background:yellow;">两种</font>值，要么是<code>True</code>，要么是<code>False</code>，在Python中，可以<font style="background:yellow;">直接用<code>True</code>、<code>False</code>表示布尔值（请注意大小写）</font></li> <li>布尔值可以用<code>and</code>、<code>or</code>和<code>not</code>运算。</li></ul> <blockquote><p><font style="background:pink;">上面是和Java和C++不同的地方</font></p></blockquote> <h3 id="_6-5-空值"><a href="#_6-5-空值" class="header-anchor">#</a> 6.5.空值</h3> <ul><li>空值是Python里一个特殊的值，用<code>None</code>表示。<code>None</code>不能理解为<code>0</code>，因为<code>0</code>是有意义的，而<code>None</code>是一个特殊的空值。</li></ul> <blockquote><p><font style="background:pink;">上面是和Java和C++不同的地方</font></p></blockquote> <blockquote><p>此外，Python还提供了列表、字典等多种数据类型，还允许创建自定义数据类型，我们后面会继续讲到。</p></blockquote> <h3 id="变量"><a href="#变量" class="header-anchor">#</a> 变量</h3> <h3 id="变量名》"><a href="#变量名》" class="header-anchor">#</a> 变量名》</h3> <ul><li><p>变量名必须是大小写英文、数字和<code>_</code>的组合，且不能用数字开头</p></li> <li><p>在Python中，等号<code>=</code>是赋值语句，可以把任意数据类型赋值给变量，同一个变量可以反复赋值，而且可以是不同类型的变量</p> <ul><li>和静态语言相比，动态语言更灵活，就是这个原因。</li></ul></li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code>a <span class="token operator">=</span> <span class="token number">123</span> 	<span class="token comment"># a是整数</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
a <span class="token operator">=</span> <span class="token string">'ABC'</span> 	<span class="token comment"># a变为字符串</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>

<span class="token comment"># 输入</span>
<span class="token comment"># 123</span>
<span class="token comment"># ABC</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="python是动态语言"><a href="#python是动态语言" class="header-anchor">#</a> Python是动态语言</h3> <ul><li>这种变量本身类型不固定的语言称之为<em>动态语言</em>，与之对应的是<em>静态语言</em>。</li></ul> <p>静态语言在定义变量时必须指定变量类型，如果赋值的时候类型不匹配，就会报错。例如Java是静态语言。</p> <h2 id="_07-常量"><a href="#_07-常量" class="header-anchor">#</a> 07.常量</h2> <ul><li>所谓常量就是不能变的变量，比如常用的数学常数π就是一个常量。<font style="background:pink;">在Python中，通常用<strong>全部大写</strong>的变量名表示常量</font> <ul><li>但事实上<code>PI</code>仍然是一个<strong>变量</strong>，<strong>Python根本没有任何机制保证<code>PI</code>不会被改变</strong>，所以，<font style="background:yellow;">用全部大写的变量名表示常量<strong>只是一个习惯上的用法</strong>，如果你一定要改变变量<code>PI</code>的值，也<strong>没人能拦住你</strong>。</font></li></ul></li></ul> <h2 id="_08-python中2种除法"><a href="#_08-python中2种除法" class="header-anchor">#</a> 08.Python中2种除法</h2> <ul><li>最后解释一下整数的除法为什么也是精确的。</li> <li>在Python中，有两种除法</li></ul> <h3 id="一种除法是-："><a href="#一种除法是-：" class="header-anchor">#</a> 一种除法是<code>/</code>：</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token number">10</span> <span class="token operator">/</span> <span class="token number">3</span>
<span class="token number">3.3333333333333335</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li><code>/</code>除法计算结果是浮点数，即使是两个整数恰好整除，结果也是浮点数</li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token number">9</span> <span class="token operator">/</span> <span class="token number">3</span>
<span class="token number">3.0</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><blockquote><p><font style="background:pink;">上面是和Java和C++不同的地方</font></p></blockquote> <h3 id="还有一种除法是-，称为地板除"><a href="#还有一种除法是-，称为地板除" class="header-anchor">#</a> 还有一种除法是<code>//</code>，称为地板除</h3> <ul><li>两个整数的除法仍然是整数：</li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token number">10</span> <span class="token operator">//</span> <span class="token number">3</span>
<span class="token number">3</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><blockquote><p>联想：C语言中floor()函数：向下取整</p> <ul><li>单词，floor，n.地面；地板，楼层</li></ul></blockquote> <h2 id="字符串和编码"><a href="#字符串和编码" class="header-anchor">#</a> 字符串和编码</h2> <ul><li><p>因此，Unicode字符集应运而生。Unicode把所有语言都统一到一套编码里，这样就不会再有乱码问题了</p></li> <li><p>Unicode标准也在不断发展，但<font style="background:yellow;">最常用的是UCS-16编码，用两个字节表示一个字符（如果要用到非常偏僻的字符，就需要4个字节）</font>。现代操作系统和大多数编程语言都直接支持Unicode。</p> <ul><li>现在，捋一捋ASCII编码和Unicode编码的区别：ASCII编码是1个字节，而Unicode编码通常是2个字节。</li></ul></li> <li><p>新的问题又出现了：如果统一成Unicode编码，乱码问题从此消失了。但是，如果你写的文本基本上全部是英文的话，用Unicode编码比ASCII编码需要多一倍的存储空间，在存储和传输上就十分不划算。</p> <ul><li>所以，本着节约的精神，又出现了把Unicode编码转化为“可变长编码”的<code>UTF-8</code>编码。UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节，常用的英文字母被编码成1个字节，汉字通常是3个字节，只有很生僻的字符才会被编码成4-6个字节。如果你要传输的文本包含大量英文字符，用UTF-8编码就能节省空间：</li></ul> <h3 id="我们就可以总结一下现在计算机系统通用的字符编码工作方式："><a href="#我们就可以总结一下现在计算机系统通用的字符编码工作方式：" class="header-anchor">#</a> 我们就可以总结一下现在计算机系统通用的字符编码工作方式：</h3> <ul><li>在计算机<font style="background:yellow;">内存中，统一使用Unicode编码</font>，当需<strong>要保存到硬盘</strong>或者<strong>需要传输</strong>的时候，就转换为UTF-8编码。
<ul><li>用记事本编辑的时候，从文件读取的UTF-8字符被转换为Unicode字符到内存里，编辑完成后，保存的时候再把Unicode转换为UTF-8保存到文件：</li> <li>浏览网页的时候，服务器会把动态生成的Unicode内容转换为UTF-8再传输到浏览器：</li></ul></li></ul></li></ul> <h3 id="python的字符串"><a href="#python的字符串" class="header-anchor">#</a> Python的字符串</h3> <ul><li>在最新的<font style="background:pink;">Python 3版本中，字符串是以Unicode编码的</font>，也就是说，Python的字符串支持多语言，例如：</li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'包含中文的str'</span><span class="token punctuation">)</span>
包含中文的<span class="token builtin">str</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>对于单个字符的编码，Python提供了<code>ord()</code>函数获取字符的整数表示，<code>chr()</code>函数把编码转换为对应的字符：</li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token builtin">ord</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
<span class="token number">65</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token builtin">ord</span><span class="token punctuation">(</span><span class="token string">'中'</span><span class="token punctuation">)</span>
<span class="token number">20013</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token builtin">chr</span><span class="token punctuation">(</span><span class="token number">66</span><span class="token punctuation">)</span>
<span class="token string">'B'</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token builtin">chr</span><span class="token punctuation">(</span><span class="token number">25991</span><span class="token punctuation">)</span>
<span class="token string">'文'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><font style="background:pink;">由于Python的字符串类型是<code>str</code>，在内存中以Unicode表示，一个字符对应若干个字节</font>。如果要在网络上传输，或者保存到磁盘上，就需要把<code>str</code>变为以字节为单位的<code>bytes</code>。</p> <ul><li>Python对<code>bytes</code>类型的数据用带<code>b</code>前缀的单引号或双引号表示：</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>x = b'ABC'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>要注意区分<code>'ABC'</code>和<code>b'ABC'</code>，前者是<code>str</code>，后者虽然内容显示得和前者一样，但<code>bytes</code>的每个字符都只占用一个字节。</p> <h3 id="encode和decode函数"><a href="#encode和decode函数" class="header-anchor">#</a> encode和decode函数</h3> <p>以Unicode表示的<code>str</code>通过<code>encode()</code>方法可以编码为指定的<code>bytes</code>，例如：</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token string">'ABC'</span><span class="token punctuation">.</span>encode<span class="token punctuation">(</span><span class="token string">'ascii'</span><span class="token punctuation">)</span>
<span class="token string">b'ABC'</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token string">'中文'</span><span class="token punctuation">.</span>encode<span class="token punctuation">(</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span>
<span class="token string">b'\xe4\xb8\xad\xe6\x96\x87'</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token string">'中文'</span><span class="token punctuation">.</span>encode<span class="token punctuation">(</span><span class="token string">'ascii'</span><span class="token punctuation">)</span>
Traceback <span class="token punctuation">(</span>most recent call last<span class="token punctuation">)</span><span class="token punctuation">:</span>
  File <span class="token string">&quot;&lt;stdin&gt;&quot;</span><span class="token punctuation">,</span> line <span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">in</span> <span class="token operator">&lt;</span>module<span class="token operator">&gt;</span>
UnicodeEncodeError<span class="token punctuation">:</span> <span class="token string">'ascii'</span> codec can't encode characters <span class="token keyword">in</span> position <span class="token number">0</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">:</span> ordinal <span class="token keyword">not</span> <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ul><li>纯英文的<code>str</code>可以用<code>ASCII</code>编码为<code>bytes</code>，内容是一样的，含有中文的<code>str</code>可以用<code>UTF-8</code>编码为<code>bytes</code>。含有中文的<code>str</code>无法用<code>ASCII</code>编码，因为中文编码的范围超过了<code>ASCII</code>编码的范围，Python会报错。</li></ul> <p>在<code>bytes</code>中，无法显示为ASCII字符的字节，用<code>\x##</code>显示。</p> <ul><li>反过来，如果我们从网络或磁盘上读取了字节流，那么读到的数据就是<code>bytes</code>。要把<code>bytes</code>变为<code>str</code>，就需要用<code>decode()</code>方法：</li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token string">b'ABC'</span><span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token string">'ascii'</span><span class="token punctuation">)</span>
<span class="token string">'ABC'</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token string">b'\xe4\xb8\xad\xe6\x96\x87'</span><span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span>
<span class="token string">'中文'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果<code>bytes</code>中包含无法解码的字节，<code>decode()</code>方法会报错：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; b'\xe4\xb8\xad\xff'.decode('utf-8')
Traceback (most recent call last):
  ...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 3: invalid start byte
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果<code>bytes</code>中只有一小部分无效的字节，可以传入<code>errors='ignore'</code>忽略错误的字节：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; b'\xe4\xb8\xad\xff'.decode('utf-8', errors='ignore')
'中'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="len函数"><a href="#len函数" class="header-anchor">#</a> len函数</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code>要计算<span class="token builtin">str</span>包含多少个字符，可以用<span class="token builtin">len</span><span class="token punctuation">(</span><span class="token punctuation">)</span>函数：
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span><span class="token string">'ABC'</span><span class="token punctuation">)</span>
<span class="token number">3</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span><span class="token string">'中文'</span><span class="token punctuation">)</span>
<span class="token number">2</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><code>len()</code>函数计算的是<code>str</code>的字符数，如果换成<code>bytes</code>，<code>len()</code>函数就计算字节数：</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span><span class="token string">b'ABC'</span><span class="token punctuation">)</span>
<span class="token number">3</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span><span class="token string">b'\xe4\xb8\xad\xe6\x96\x87'</span><span class="token punctuation">)</span>
<span class="token number">6</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token builtin">len</span><span class="token punctuation">(</span><span class="token string">'中文'</span><span class="token punctuation">.</span>encode<span class="token punctuation">(</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token number">6</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li><p>由于Python源代码也是一个文本文件，所以，当你的源代码中包含中文的时候，在保存源代码时，就需要务必指定保存为UTF-8编码。当Python解释器读取源代码时，为了让它按UTF-8编码读取，我们通常在文件开头写上这两行：</p> <ul><li><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#!/usr/bin/env python3</span>
<span class="token comment"># -*- coding: utf-8 -*-</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul></li> <li><p>第一行注释是为了告诉Linux/OS X系统，这是一个Python可执行程序，Windows系统会忽略这个注释；</p></li> <li><p>第二行注释是为了告诉Python解释器，按照UTF-8编码读取源代码，否则，你在源代码中写的中文输出可能会有乱码。</p></li> <li><p>申明了UTF-8编码并不意味着你的<code>.py</code>文件就是UTF-8编码的，必须并且要确保文本编辑器正在使用UTF-8 without BOM编码</p></li></ul> <h3 id="格式化"><a href="#格式化" class="header-anchor">#</a> 格式化</h3> <ul><li>在Python中，采用的格式化方式<font style="background:yellow;">和C语言是一致的，用<code>%</code>实现</font> <ul><li>但是用法和C语言有点不一样</li> <li>顺序要对应好。如果只有一个<code>%d</code>、<code>%f</code>啥的，括号可以省略。</li> <li>如果你不太确定应该用什么，<code>%s</code>永远起作用，它会把任何数据类型转换为字符串：
<ul><li>有些时候，字符串里面的<code>%</code>是一个普通字符怎么办？这个时候就需要转义，用<code>%%</code>来表示一个<code>%</code>：</li></ul></li></ul></li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token string">'Hello, %s'</span> <span class="token operator">%</span> <span class="token string">'world'</span>
<span class="token string">'Hello, world'</span>
<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token string">'Hi, %s, you have $%d.'</span> <span class="token operator">%</span> <span class="token punctuation">(</span><span class="token string">'Michael'</span><span class="token punctuation">,</span> <span class="token number">1000000</span><span class="token punctuation">)</span>
<span class="token string">'Hi, Michael, you have $1000000.'</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">print</span><span class="token punctuation">(</span> <span class="token string">&quot;%s 222&quot;</span> <span class="token operator">%</span> <span class="token string">&quot;1111&quot;</span><span class="token punctuation">)</span>
<span class="token comment">#输出1111 222</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-python line-numbers-mode"><pre class="language-python"><code>a<span class="token operator">=</span><span class="token number">15</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;%d + %x &quot;</span> <span class="token operator">%</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#输出15 + f</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>常见的占位符有：</p> <table><thead><tr><th style="text-align:left">占位符</th> <th style="text-align:left">替换内容</th></tr></thead> <tbody><tr><td style="text-align:left">%d</td> <td style="text-align:left">整数</td></tr> <tr><td style="text-align:left">%f</td> <td style="text-align:left">浮点数</td></tr> <tr><td style="text-align:left">%s</td> <td style="text-align:left">字符串</td></tr> <tr><td style="text-align:left">%x</td> <td style="text-align:left">十六进制整数</td></tr></tbody></table> <h3 id="format-「我不打算用这个」"><a href="#format-「我不打算用这个」" class="header-anchor">#</a> format()「我不打算用这个」</h3> <ul><li>另一种格式化字符串的方法是使用字符串的<code>format()</code>方法，它会用传入的参数依次替换字符串内的占位符<code>{0}</code>、<code>{1}</code>……，不过这种方式写起来比%要麻烦得多：</li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token operator">&gt;&gt;</span><span class="token operator">&gt;</span> <span class="token string">'Hello, {0}, 成绩提升了 {1:.1f}%'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'小明'</span><span class="token punctuation">,</span> <span class="token number">17.125</span><span class="token punctuation">)</span>
<span class="token string">'Hello, 小明, 成绩提升了 17.1%'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="f-string「我也还是不常用这个」"><a href="#f-string「我也还是不常用这个」" class="header-anchor">#</a> f-string「我也还是不常用这个」</h3> <p>最后一种格式化字符串的方法是使用以<code>f</code>开头的字符串，称之为<code>f-string</code>，它和普通字符串不同之处在于，字符串如果包含<code>{xxx}</code>，就会以对应的变量替换：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; r = 2.5
&gt;&gt;&gt; s = 3.14 * r ** 2
&gt;&gt;&gt; print(f'The area of a circle with radius {r} is {s:.2f}')
The area of a circle with radius 2.5 is 19.62
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上述代码中，<code>{r}</code>被变量<code>r</code>的值替换，<code>{s:.2f}</code>被变量<code>s</code>的值替换，并且<code>:</code>后面的<code>.2f</code>指定了格式化参数（即保留两位小数），因此，<code>{s:.2f}</code>的替换结果是<code>19.62</code>。</p> <h2 id="✅判断和循环"><a href="#✅判断和循环" class="header-anchor">#</a> ✅判断和循环</h2> <h2 id="条件判断"><a href="#条件判断" class="header-anchor">#</a> 条件判断</h2> <h3 id="if语句"><a href="#if语句" class="header-anchor">#</a> if语句</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code>age <span class="token operator">=</span> <span class="token number">3</span>
<span class="token keyword">if</span> age <span class="token operator">&gt;=</span> <span class="token number">18</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'adult'</span><span class="token punctuation">)</span>
<span class="token keyword">elif</span> age <span class="token operator">&gt;=</span> <span class="token number">6</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'teenager'</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'kid'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>elif是else if的缩写，完全可以有多个elif</p> <ul><li><font style="background:yellow;">只要<code>x</code>是非零数值、非空字符串、非空list等</font>，就判断为<code>True</code>，否则为<code>False</code>。</li></ul> <h3 id="跟其它语言有所区别，python中并没有switch-case语句"><a href="#跟其它语言有所区别，python中并没有switch-case语句" class="header-anchor">#</a> 跟其它语言有所区别，Python中并没有Switch/Case语句!</h3> <h3 id="input函数的纠偏"><a href="#input函数的纠偏" class="header-anchor">#</a> input函数的纠偏</h3> <p>因为<code>input()</code>返回的数据类型是<code>str</code>，<code>str</code>不能直接和整数比较，必须先把<code>str</code>转换成整数。</p> <ul><li><p>Python提供了<code>int()</code>函数来完成这件事情：</p></li> <li><p>whoway：我觉得这个有点像C++11中的int强制类型转化？</p></li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code>s <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">'birth: '</span><span class="token punctuation">)</span>
birth <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span>
<span class="token keyword">if</span> birth <span class="token operator">&lt;</span> <span class="token number">2000</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'00前'</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'00后'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>再次运行，就可以得到正确地结果。但是，如果输入<code>abc</code>呢？又会得到一个错误信息：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
ValueError: invalid literal for int() with base 10: 'abc'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li><font style="background:yellow;">原来<code>int()</code>函数发现一个字符串<strong>并不是合法的数字</strong>时就会<strong>报错</strong>，程序就退出了。</font></li></ul> <p>如何检查并捕获程序运行期的错误呢？后面的错误和调试会讲到。</p> <h2 id="循环"><a href="#循环" class="header-anchor">#</a> 循环</h2> <ul><li>Python的循环有两种</li></ul> <h3 id="for-in循环"><a href="#for-in循环" class="header-anchor">#</a> for...in循环</h3> <ul><li>一种是for...in循环，依次把<font style="background:yellow;">list或tuple</font>中的每个元素迭代出来
<ul><li>所以<code>for x in ...</code>循环就是把每个元素代入变量<code>x</code>，然后执行缩进块的语句。</li></ul></li> <li>whoway：
<ul><li>这种循环类似C++11中for( auto num : array )的循环</li> <li>类似Java中的for each循环</li></ul></li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code>names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'Michael'</span><span class="token punctuation">,</span> <span class="token string">'Bob'</span><span class="token punctuation">,</span> <span class="token string">'Tracy'</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> name <span class="token keyword">in</span> names<span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>如果要计算1-100的整数之和，从1写到100有点困难
<ul><li>幸好Python提供一个<code>range()</code>函数，可以生成一个整数序列</li> <li>再通过<code>list()</code>函数可以转换为list。比如<code>range(5)</code>生成的序列是<font style="background:yellow;">从0开始小于5</font>的整数：</li></ul></li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token builtin">sum</span> <span class="token operator">+</span> x
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">sum</span><span class="token punctuation">)</span>

<span class="token comment"># 输出 3</span>
<span class="token comment"># 0+0+1+2</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
    <span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token builtin">sum</span> <span class="token operator">+</span> x
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">sum</span><span class="token punctuation">)</span>
<span class="token comment">#输出55</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="while循环"><a href="#while循环" class="header-anchor">#</a> while循环</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token number">0</span>
n <span class="token operator">=</span> <span class="token number">99</span>
<span class="token keyword">while</span> n <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
    <span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token builtin">sum</span> <span class="token operator">+</span> n
    n <span class="token operator">=</span> n <span class="token operator">-</span> <span class="token number">2</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">sum</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>下面是死循环</li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;yes&quot;</span><span class="token punctuation">)</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="关键字break和continue"><a href="#关键字break和continue" class="header-anchor">#</a> 关键字break和continue</h4> <ul><li>break在循环中，break语句可以提前退出循环</li> <li>continue在循环过程中，也可以通过<code>continue</code>语句，跳过当前的这次循环，直接开始下一次循环。</li></ul> <h3 id="在-python-中没有-do-while-循环！"><a href="#在-python-中没有-do-while-循环！" class="header-anchor">#</a> 在 Python 中没有 do..while 循环！</h3> <h2 id="✅函数"><a href="#✅函数" class="header-anchor">#</a> ✅函数</h2> <ul><li>基本上所有的高级语言都支持函数，Python也不例外。Python不但能非常灵活地定义函数，而且本身内置了很多有用的函数，可以直接调用。</li></ul> <h3 id="help函数的使用"><a href="#help函数的使用" class="header-anchor">#</a> help函数的使用</h3> <p>也可以在交互式命令行通过<code>help(abs)</code>查看<code>abs</code>函数的帮助信息。</p> <h3 id="数据类型转换"><a href="#数据类型转换" class="header-anchor">#</a> 数据类型转换</h3> <p>Python内置的常用函数还包括数据类型转换函数，比如<code>int()</code>函数可以把其他数据类型转换为整数：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; int('123')
123
&gt;&gt;&gt; int(12.34)
12
&gt;&gt;&gt; float('12.34')
12.34
&gt;&gt;&gt; str(1.23)
'1.23'
&gt;&gt;&gt; str(100)
'100'
&gt;&gt;&gt; bool(1)
True
&gt;&gt;&gt; bool('')
False
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><ul><li>函数名其实就是指向一个<strong>函数对象</strong>的引用，完全可以把函数名赋给一个变量，相当于给这个函数起了一个“别名”：
<ul><li>whoway：这个可以和C++中的「函数对象」类似</li></ul></li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; a = abs # 变量a指向abs函数
&gt;&gt;&gt; a(-1) # 所以也可以通过a调用abs函数
1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_1-定义函数"><a href="#_1-定义函数" class="header-anchor">#</a> 1.定义函数</h3> <ul><li>在Python中，定义一个函数要使用<code>def</code>语句
<ul><li>依次写出函数名、括号、括号中的参数和冒号<code>:</code></li> <li>然后，在缩进块中编写函数体，函数的返回值用<code>return</code>语句返回。</li></ul></li></ul> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">myabs</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> x <span class="token operator">&gt;=</span> <span class="token number">0</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> x
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> <span class="token operator">-</span>x

<span class="token keyword">print</span><span class="token punctuation">(</span> myabs<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>在Python交互环境中定义函数时，注意Python会出现<code>...</code>的提示。函数定义结束后需要按两次回车重新回到<code>&gt;&gt;&gt;</code>提示符下：</li></ul> <h4 id="_1-1-空函数"><a href="#_1-1-空函数" class="header-anchor">#</a> 1.1.空函数</h4> <p>如果想定义一个什么事也不做的空函数，可以用<code>pass</code>语句：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>def nop():
    pass
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>pass</code>语句什么都不做，那有什么用？实际上<code>pass</code>可以用来作为占位符，比如现在还没想好怎么写函数的代码，就可以先放一个<code>pass</code>，让代码能运行起来。</p> <p><code>pass</code>还可以用在其他语句里，比如：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>if age &gt;= 18:
    pass
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>缺少了<code>pass</code>，代码运行就会有语法错误。</p> <h4 id="_1-2-参数检查"><a href="#_1-2-参数检查" class="header-anchor">#</a> 1.2.参数检查</h4> <p><font style="background:yellow;">调用函数时，如果参数个数不对，Python解释器会自动检查出来</font>，并抛出<code>TypeError</code>：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; my_abs(1, 2)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
TypeError: my_abs() takes 1 positional argument but 2 were given
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>但是如果参数类型不对，Python解释器就无法帮我们检查。试试<code>my_abs</code>和内置函数<code>abs</code>的差别：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; my_abs('A')
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;&lt;stdin&gt;&quot;, line 2, in my_abs
TypeError: unorderable types: str() &gt;= int()
&gt;&gt;&gt; abs('A')
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
TypeError: bad operand type for abs(): 'str'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>当传入了不恰当的参数时，内置函数<code>abs</code>会检查出参数错误，而我们定义的<code>my_abs</code>没有参数检查，会导致<code>if</code>语句出错，出错信息和<code>abs</code>不一样。所以，这个函数定义不够完善。</p> <p>让我们修改一下<code>my_abs</code>的定义，<font style="background:yellow;">对参数类型做检查，<strong>只允许整数和浮点数类型</strong>的参数。数据类型检查可以用内置函数<code>isinstance()</code>实现</font>：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>def my_abs(x):
    if not isinstance(x, (int, float)):
        raise TypeError('bad operand type')
    if x &gt;= 0:
        return x
    else:
        return -x
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>添加了参数检查后，如果传入错误的参数类型，函数就可以抛出一个错误：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; my_abs('A')
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;&lt;stdin&gt;&quot;, line 3, in my_abs
TypeError: bad operand type
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>错误和异常处理将在后续讲到。</p> <h4 id="_1-3-返回多个值"><a href="#_1-3-返回多个值" class="header-anchor">#</a> 1.3.返回多个值</h4> <p>函数可以返回多个值吗？答案是肯定的。</p> <p>比如在游戏中经常需要从一个点移动到另一个点，给出坐标、位移和角度，就可以计算出新的坐标：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>import math

def move(x, y, step, angle=0):
    nx = x + step * math.cos(angle)
    ny = y - step * math.sin(angle)
    return nx, ny
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>==<code>import math</code>语句表示导入<code>math</code>包，并允许后续代码引用<code>math</code>包里的<code>sin</code>、<code>cos</code>等函数。==</li></ul> <p>然后，我们就可以同时获得返回值：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; x, y = move(100, 100, 60, math.pi / 6)
&gt;&gt;&gt; print(x, y)
151.96152422706632 70.0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>但其实这只是一种假象，Python函数返回的仍然是单一值：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&gt;&gt;&gt; r = move(100, 100, 60, math.pi / 6)
&gt;&gt;&gt; print(r)
(151.96152422706632, 70.0)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>原来返回值是一个tuple！但是，在语法上，返回一个tuple可以省略括号，而多个变量可以同时接收一个tuple，按位置赋给对应的值，所以，Python的函数返回多值其实就是返回一个tuple，但写起来更方便。</p> <h3 id="_2-函数的参数"><a href="#_2-函数的参数" class="header-anchor">#</a> 2.函数的参数</h3> <ul><li>难点<a href="https://www.liaoxuefeng.com/wiki/1016959663602400/1017261630425888" target="_blank" rel="noopener noreferrer">传送门<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h3 id="import的用法在后续模块一节中会详细介绍。"><a href="#import的用法在后续模块一节中会详细介绍。" class="header-anchor">#</a> <code>import</code>的用法在后续<a href="https://www.liaoxuefeng.com/wiki/1016959663602400/1017454145014176" target="_blank" rel="noopener noreferrer">模块<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>一节中会详细介绍。</h3> <ul><li>解决递归调用栈溢出的方法是通过<strong>尾递归</strong>优化，事实上尾递归和循环的效果是一样的，所以，把循环看成是一种特殊的尾递归函数也是可以的。</li> <li>尾递归是指，在函数返回的时候，调用自身本身，并且，return语句不能包含表达式。这样，编译器或者解释器就可以把尾递归做优化，使递归本身无论调用多少次，都只占用一个栈帧，不会出现栈溢出的情况。</li> <li>尾递归调用时，如果做了优化，栈不会增长，因此，无论多少次调用也不会导致栈溢出。</li> <li><font style="background:yellow;">遗憾的是，<strong>大多数编程语言没有针对尾递归做优化</strong>，Python解释器也没有做优化</font>，所以，即使把上面的<code>fact(n)</code>函数改成尾递归方式，也会导致栈溢出。</li></ul> <h2 id="✅模块"><a href="#✅模块" class="header-anchor">#</a> ✅模块</h2> <ul><li>为了编写可维护的代码，我们把很多函数分组，分别放到不同的文件里，这样，每个文件包含的代码就相对较少，很多编程语言都采用这种组织代码的方式。</li> <li><strong>在Python中，一个.py文件就称之为一个模块（Module）</strong>。</li></ul></div> <div class="page-edit"><!----> <!----></div> <!----> </div> <!----></div></div>
    <script src="/assets/js/app.c70e21ad.js" defer></script><script src="/assets/js/42.76f52d62.js" defer></script>
  </body>
</html>
